﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.Web.UI.Controls;
using ZX.KM.Core.Permission;
using ZX.KM.Service.Messages;
using ZX.KM.Service.Criterias;
using ZX.KM.Service.ServiceProxy;
using ZX.KM.Service.Contracts;
using ZX.KM.Service.DataTransferObjects;
using System.Configuration;
using log4net;

namespace ZX.WebApp.KM.FullTextSearch
{
    public partial class FullTextSearchList : KmBasePage
    {
        string _Action = string.Empty;
        string km_i = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            _Action = QueryString("Action");
            switch (_Action)
            {
                case "GetSearchResultByKey":
                    GetSearchResultByKey();
                    break;
                case "CheckRight":
                    try
                    {
                        string ResourceIDStr = QueryString("ResourceID");
                        Guid ResourceID = ResourceIDStr == "" ? Guid.Empty : new Guid(ResourceIDStr);

                        ErrorModel em = new ErrorModel();
                        if (CheckRight(ResourceID, PermissionConstants.Doc_View))
                        {
                            em.ErrorType = "";
                            em.ErrorMsg = "yes";
                        }
                        else
                        {
                            em.Code = Guid.NewGuid().ToString();
                            em.ErrorType = "-10001";
                            em.ErrorMsg = "no";
                            em.ErrorMsg = "您没有权限查看该文档，请联系管理授权.";
                        }
                        ResponseJson(em);
                    }
                    catch (Exception ex)
                    {
                        var logger = LogManager.GetLogger(GetType());
                        logger.Error(ex.Message, ex);

                        ErrorModel em = new ErrorModel();
                        em.Code = Guid.NewGuid().ToString();
                        em.ErrorType = "-10001";
                        em.ErrorMsg = "no";
                        em.ErrorMsg = "您没有权限查看该文档，请联系管理授权.";
                        ResponseJson(em);
                    }
                    break;

            }
        }

        public string KmIndex
        {
            get
            {
                string KMINDEX = this.GetCurrentHost + "/kM/index.aspx";
                return KMINDEX;
            }
        }

        public string KmLoadFromOut
        {
            get
            {
                string LoadFromOut = this.GetCurrentHost + "/km/LoadFromOut.aspx";
                return LoadFromOut;
            }
        }

        private void GetSearchResultByKey()
        {
            try
            {
                string key = Server.UrlDecode(QueryString("Key"));
                int index = Request["PageIndex"] == null ? 1 : int.Parse(Request["PageIndex"]);
                int size = Request["PageSize"] == null ? 1 : int.Parse(Request["PageSize"]);

                FullTextSearchRequest fr = new FullTextSearchRequest
                {
                    Body = new FullTextSearchCriteria
                    {
                        Key = key,
                        PageIndex = index,
                        PageSize = size
                    }
                };
                fr.Header = CurrentRequestHeader;
                IHomePageService target = ServiceProxyFactory.GetService<IHomePageService>();
                FullTextSearchResponse actual = target.FullTextSearch(fr);

                List<ResultInfo> list = actual.Body.Result.ToList<ResultInfo>();
                List<ResultInfo> newList = new List<ResultInfo>();

                var UserID = this.UserId.Value.ToString();
                if (list.Count > 0)
                {
                    foreach (ResultInfo info in list)
                    {
                        ResultInfo tempInof = new ResultInfo();
                        tempInof = info;

                        var DocumentID = info.ID;
                        var newObj = "{\"DocumentID\":\"" + DocumentID + "\",\"SearchType\":1,\"typeid\":\"\",\"leftid\":\"\",\"UserID\":\"" + UserID + "\"}";
                        newObj = ZX.KM.Core.Common.SecurityUtility.Encrypt(newObj, web_url_key);
                        newObj = HttpUtility.UrlEncode(newObj);
                        tempInof.CryptographyValue = newObj;
                        newList.Add(tempInof);
                    }
                }

                FullTextSearchResponse.FullTextSearchBody body = new FullTextSearchResponse.FullTextSearchBody();
                body = actual.Body;
                body.Result = newList;
                ResponseJson(body);
            }
            catch (Exception ex)
            {
                var logger = LogManager.GetLogger(GetType());
                logger.Error(ex.Message, ex);
            }
        }
    }
}